<%@ page import="java.util.*"%>
<%@ page import="objects.*"%>
<%@ page import="Domain.UserClient.ButtonsFunctions"%>
<%@page import="Domain.DataManager"%>

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>

<%
	DataManager dm = (DataManager) (session.getAttribute("dm"));
	ButtonsFunctions bf = (ButtonsFunctions) (session
			.getAttribute("bf"));

	if (dm == null || dm.getUserStatus() == DataManager.DISCONNECTED) {
		response.sendRedirect("Welcome.jsp");
	} else {
%>

<%
	dm.setCurrThread(null);
%>

<%
	String nameOfForum = request.getParameter("nameOfForum");
		dm.setCurrForum(nameOfForum);
%>



<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet" href="style.css" type="text/css">

<title>Choose Thread</title>

<link rel="stylesheet" href="windowfiles/dhtmlwindow.css" type="text/css" />

<script type="text/javascript" src="windowfiles/dhtmlwindow.js"></script>

<script language="javascript" type="text/javascript">
		
		//Browser Support Code
		function updateUsingAjax() {
			var ajaxRequest;

			// The variable that makes Ajax possible! 
			try {
				// Opera 8.0+, Firefox, Safari 
				ajaxRequest = new XMLHttpRequest();
			} catch (e) {
				// Internet Explorer Browsers 
				try {
					ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
				} catch (e) {
					try {
						ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
					} catch (e) {
						// Something went wrong 
						alert("Your browser broke!");
						return false;
					}
				}
			}
			
			// Create a function that will receive data sent from the server 
			ajaxRequest.onreadystatechange = function() {
				if (ajaxRequest.readyState == 4) {
					var response = ajaxRequest.responseText;			
					if(response.trim() == "")
					{
						alert("You have been disconnected from the server!");
						parent.location = 'Welcome.jsp';
					}
					else
						document.getElementById("numOfNotifications").innerHTML = response;
				}
			};

			var url = "GetNumOfNotifications.jsp";
			var params = "";
			ajaxRequest.open("POST", url, true);

			//Send the proper header information along with the request
			ajaxRequest.setRequestHeader("Content-type",
					"application/x-www-form-urlencoded");
			ajaxRequest.setRequestHeader("Content-length", params.length);
			ajaxRequest.setRequestHeader("Connection", "close");

			ajaxRequest.send(params);
		}

		function updateNumOfNotifications() {
			updateUsingAjax();
			setTimeout("updateNumOfNotifications()", 3000);
		}
		
		function openmypage() { //Define arbitrary function to run desired DHTML Window widget codes
			ajaxwin = dhtmlwindow.open("ajaxbox", "ajax", "GetNotification.jsp", 
					"Notification window", "width=450px,height=300px,left=300px,top=100px,resize=1,scrolling=1");
			ajaxwin.onclose = function() {
				return window.confirm("Close notification window?");
			}
		}
		
	</script>

</head>
<body>

	<table width="100%" height="100%" border="1" cellspacing="0"
		cellpadding="0">

		<!-- HEADER -->
		<tr class="header">
			<td>
				<!-- cool navigation toolbar code -->
				<ul id="navlist">
					<li id="logout"><a href="Logout.jsp"></a></li>
					<li id="prev"><a href="ChooseForum.jsp"></a></li>
					<li id="notifications"><a href="#" onClick="openmypage(); return false"></a></li>
					<li id="notificationCounter"><center>
							<font size=3 id="numOfNotifications"></font>
						</center>
					</li>

					<li id="currentForum">Current Forum is: <b><i><font
								color='#FF0000'> <%
 	out.print(dm.getCurrForum());
 %> </font> </i> </b></li>
					<li id="connection">Connected as: <b><i><font
								color='#FF0000'> <%
 	out.print(dm.getCurrUser());
 %> </font> </i> </b></li>
				</ul>
			</td>
		</tr>

		<!-- BODY -->
		<tr class="body">
			<td class="forumBody">

				<center>
					<h6>
						<font color="#FFF11" size="7"><i>Please choose a thread</i>
						</font>
					</h6>
				</center>

				<center>
					<%
						//-----------------show the relevant buttons-------------------

							bf.processGetTypeOfUser();
							int userType = dm.getTypeOfUser();

							if (userType == 1) {
								out.print("<button type=\"button\" onClick=\"parent.location = 'AddThread.jsp';\">Add Thread</button>");
							}
					%>
				</center> <br>
			<br>
			<br>
				<div align="center">

					<%
						//-----------------Print the list of forums-------------------
							boolean hasElements = true;

							bf.processGetThreads(nameOfForum);

							Vector<Trd> threads = dm.getVectorThreads();
							if (threads == null)
								System.out.println("vector of threads didn't arrive!");
							else {
								if (threads.size() > 0) {
									for (int i = 0; i < threads.size(); i++) {
										String t = threads.elementAt(i).getThreadName();
										out.println("<p class = 'list'><a class='names' href=\"ChoosePost.jsp?nameOfThread="
												+ t + "\"><b><font size = 5>" + t + "</font></b></a></p>");
									}
								} else
									out.println("<b><font color = #FFAA44> there are no threads to show!</font></b>");
							}
					%>
				</div></td>
		</tr>
	</table>
	<script language="javascript" type="text/javascript">updateNumOfNotifications();</script>
</body>
</html>
<%
	}
%>